<?php
require_once __DIR__.'/../vendor/autoload.php';
$config = require('./config.php');

use Overtrue\Socialite\SocialiteManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Predis\Client;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;

$request = Request::createFromGlobals();
$session = new Session();
$session->start();
$request->setSession($session);

try {
    $socialite = new SocialiteManager(['wechat' => $config['wechat']]);
    $user = $socialite->driver('wechat')->setRequest($request)->user();
} catch (Exception $e) {
    
}

$userInfo = $user->getOriginal();
$key = 'user:'.$user->getId();
$redis = new Client($config['redis']);
$userInfo['timestamp'] = time();
$userInfo = json_encode($userInfo);
$redis->set($key, $userInfo);
$redis->expire($key, 60);
$url = $config['stu_helper']['callback'].'?openid='.$user->getId().'&state='.$request->getSession()->get('rand_str', '');
$redirect = new RedirectResponse($url);
$redirect->send();
?>
